<?php

namespace app\common\model;

use think\Model;

class WechatApplet extends Model
{
    // 表名
    protected $name = 'wechat_applet';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $resultSetType = 'collection';
    
    // 追加属性
    protected $append = [
        'typedata_text',
        'state_text',
        'deletetime_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function getTypedataList()
    {
        return ['serv_account' => __('Typedata serv_account'),'miniapp' => __('Typedata miniapp'),'sub_account' => __('Typedata sub_account')];
    }     

    public function getStateList()
    {
        return ['enable' => __('State enable'),'disable' => __('State disable'),'unaudit' => __('State unaudit')];
    }     

    //是否需要验证关注公众号
    public function getFollowList()
    {
        return ['0' => '否' , '1' => '是'];
    }

    public function getTypedataTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['typedata'];
        $list = $this->getTypedataList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStateTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['state'];
        $list = $this->getStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getDeletetimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['deletetime'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setDeletetimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }


}
